//
//  WorkViewController.m
//  PropertyMananger
//
//  Created by bh on 16/12/21.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "WorkViewController.h"
#import "WorkCell.h"

#import "RepairController.h"
#import "StatisticsController.h"
#import "ComplainController.h"
#import "HistoryWorkController.h"

@interface WorkViewController ()<UITableViewDelegate,UITableViewDataSource>
/**工单列表**/
@property (nonatomic,strong) UITableView * mTableView;
/**图片数据源**/
@property (nonatomic,strong) NSArray * mImgArr;
/**标题数据源**/
@property (nonatomic,strong) NSArray * mTitleArr;
@property (nonatomic,copy) NSString * complainttotalCount;//总投诉单数量
@property (nonatomic,copy) NSString * repairtotalCount;//总报修单单数量

@property (nonatomic,copy) NSString * myComplaintcount;//我的投诉单数量
@property (nonatomic,copy) NSString * myRepaircount;//我的报修单单数量

//字符串拼接
@property (nonatomic,copy) NSString * titleStr;//标题
@property (nonatomic,copy) NSString * pingjieStr;//拼接标题
@property (nonatomic,strong) NSMutableAttributedString * stra;

@end

@implementation WorkViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [super viewWillAppear:YES];
    
    //设置icon 小标为0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [JPUSHService setBadge:0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    [self postWorkNumData];
    [MBProgressHUD hideHUDForView:self.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self crateTableView];
    
    [self initData];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [self postWorkNumData];
    
    DLog(@" == %@",self.repairtotalCount);
}
- (void)crateTableView
{

    self.mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, BHscreenH - 44) style:UITableViewStylePlain];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    self.mTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.mTableView.backgroundColor = BHGrayColor;
    [self.view addSubview:self.mTableView];
}
#pragma makr - 数据源
- (void)initData
{
//icon
    self.mImgArr = @[@"ico_work_order_repair",@"ico_work_order_complaint",@"ico_work_order_history",@"ico_work_order_statistics"];
    //标题
    self.mTitleArr = @[@"报修单",@"投诉单",@"历史工单",@"统计"];
    

}
#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
    return self.mImgArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkCell * workCell = [WorkCell cellWithTableView:tableView];
    workCell.iconImg.image = [UIImage imageNamed:self.mImgArr[indexPath.row]];
    workCell.titleLabel.text = self.mTitleArr[indexPath.row];
    
    
    
    if (indexPath.row == 0)
    {
        if (self.repairtotalCount == nil) {
             workCell.subTitle.text = @"未处理";
             workCell.messageCountLab.text = [NSString stringWithFormat:@"%@  条",@" "];
        }else{
        
            workCell.subTitle.text = @"未处理";
            workCell.messageCountLab.text = [NSString stringWithFormat:@"%@  条",self.repairtotalCount];
          
        }
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:workCell.messageCountLab.text];
        [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,workCell.messageCountLab.text.length - 1)];
        workCell.messageCountLab.attributedText = string;
        
    }else if (indexPath.row == 1)
    {
        if (self.complainttotalCount == nil) {
            
            workCell.subTitle.text = @"未处理";
            workCell.messageCountLab.text = [NSString stringWithFormat:@"%@  条",@"  "];
        }else{
        
            workCell.subTitle.text = @"未处理";
            workCell.messageCountLab.text = [NSString stringWithFormat:@"%@  条",self.complainttotalCount];
        }
        
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:workCell.messageCountLab.text];
        [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,workCell.messageCountLab.text.length - 1)];
        workCell.messageCountLab.attributedText = string;
    }
    
    
    return workCell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        
        RepairController * repairVC = [[RepairController alloc]init];
        
        [self.navigationController pushViewController:repairVC animated:YES];
    }else if (indexPath.row == 1){
    
        ComplainController * complainVC = [[ComplainController alloc]init];
        [self.navigationController pushViewController:complainVC animated:YES];
    }else if (indexPath.row == 2){
    
        HistoryWorkController * historyVC = [[HistoryWorkController alloc]init];
        [self.navigationController pushViewController:historyVC animated:YES];
    }else if (indexPath.row == 3){
    
        StatisticsController * statisticsVC = [[StatisticsController alloc] init];
        [self.navigationController pushViewController:statisticsVC animated:YES];
        
    }
    DLog(@"点击第%ld行",indexPath.row + 1);
}
#pragma mark - 获取数据
- (void)postWorkNumData{

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    
    NSDictionary * parms = nil;
    parms = @{
              @"WorkerId":userModel.WorkerID,
              @"CommunityID":userModel.CommunityID,
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"incompletetask\",\"version\":\"V1.0\"}"
              };
    
    [WorkOrderRequest postWorkNumSuccess:^(id data) {
    
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"工单数量的数据 == %@",array);
        for (NSDictionary * dict in array) {
            
            self.complainttotalCount = [NSString stringWithFormat:@"%@",dict[@"AllComplaintcount"]];
            self.repairtotalCount = [NSString stringWithFormat:@"%@",dict[@"AllRepaircount"]];
            DLog(@"报修单数量 == %@",self.repairtotalCount);
            
        }
        
        [self.mTableView reloadData];
        
        [MBProgressHUD hideHUDForView:self.view];
    } Fail:^(NSString *errorDes) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD hideHUD];
    } param:parms];

}
@end
